package net.ecpt.hrm.services;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.wvtools.utils.ExcelPoiUtils;

import javolution.util.FastList;
import javolution.util.FastMap;

public class HrBecomeFullMemberXlsServices {
    public static final String module = HrBecomeFullMemberXlsServices.class.getName();


    /**
     * 导入
     */
    public static Map<String, Object> importXls(DispatchContext dctx, Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
	Delegator delegator = dctx.getDelegator();
	List<GenericValue> toBeStore = new ArrayList();
	List<String> messageList = new ArrayList<String>();
	List<Map<String, Object>> sheetList = (List<Map<String, Object>>) context.get("sheetList");
	LocalDispatcher dispatcher = dctx.getDispatcher();
	GenericValue userLogin = (GenericValue) context.get("userLogin");
	String createBy = userLogin.getString("partyId");
//	Map service_context = UtilMisc.toMap("userLogin", userLogin, "partyId", partyId);
	
	try {
	    int i = 0;
	   
	    for (Map<String, Object> row : sheetList) {
		i = i + 1;
		
			String id = ExcelPoiUtils.getCellString(row, 0);
			String partyId_dicText = ExcelPoiUtils.getCellString(row, 1);
			String departmentId_dicText = ExcelPoiUtils.getCellString(row, 2);
			String jobId = ExcelPoiUtils.getCellString(row, 3);
			Timestamp becomeDate = ExcelPoiUtils.getCellTimestamp(row, 4);
			String description = ExcelPoiUtils.getCellString(row, 5);
			//"编号,员工姓名,部门,职位,转正日期,转正原因
			
			// 判断
			if (UtilValidate.isEmpty(partyId_dicText)) {
			    messageList.add("第" + (i + 1) + "行,员工姓名不能为空。");
			    continue;
			}

			// 判断
			if (UtilValidate.isEmpty(departmentId_dicText)) {
			    messageList.add("第" + (i + 1) + "行,部门不能为空。");
			    continue;
			}
			// 判断
			if (UtilValidate.isEmpty(jobId)) {
			    messageList.add("第" + (i + 1) + "行,职位不能为空。");
			    continue;
			}
			// 判断
			if (UtilValidate.isEmpty(becomeDate)) {
			    messageList.add("第" + (i + 1) + "行,转正日期不能为空。");
			    continue;
			}

			if (UtilValidate.isEmpty(description)) {
			    messageList.add("第" + (i + 1) + "行,转正原因不能为空。");
			    continue;
			}

			
			// 创建
			GenericValue entity = null;
			if (UtilValidate.isNotEmpty(id)) {
			   
			    List<GenericValue> entityList = delegator.findByAnd("HrBecomeFullMember",
					    UtilMisc.toMap("id", id), null, false);
				    entity = EntityUtil.getFirst(entityList);
				if (UtilValidate.isEmpty(entity)) {
						 id = delegator.getNextSeqId("HrBecomeFullMember");
						 entity = delegator.makeValue("HrBecomeFullMember", UtilMisc.toMap("id", id));
						 entity.set("statusId", "0");
							entity.set("createBy", createBy);
							entity.set("createDate", nowTimestamp);
				}
					   
			}else  {
			    id = delegator.getNextSeqId("HrBecomeFullMember");
			    entity = delegator.makeValue("HrBecomeFullMember", UtilMisc.toMap("id", id));
			    entity.set("statusId", "0");
				entity.set("createBy", createBy);
				entity.set("createDate", nowTimestamp);
			
			} 
			
			
			
			
			List<GenericValue> personInfo = delegator.findByAnd("Person", UtilMisc.toMap("name", partyId_dicText),null, false);
			GenericValue person =EntityUtil.getFirst(personInfo);
			String partyId = person.getString("partyId");
			
			List<GenericValue> hrDepartmentInfo = delegator.findByAnd("HrDepartment", UtilMisc.toMap("name", departmentId_dicText),null, false);
			GenericValue hrDepartment =EntityUtil.getFirst(hrDepartmentInfo);
			String departmentId = hrDepartment.getString("departmentId");
			
			
			entity.set("partyId", partyId);
			entity.set("departmentId", departmentId);
			entity.set("jobId", jobId);
			entity.set("becomeDate", becomeDate);
			entity.set("description", description);
			
			
			toBeStore.add(entity);
			

			
		}
	   
	    delegator.storeAll(toBeStore);
	    result.put("messageList", messageList);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	return result;
    }

    /**
     * 导出
     */

    public static Map<String, Object> exportXls(DispatchContext dctx, Map<String, ? extends Object> context) {
	Delegator delegator = dctx.getDelegator();
	String ids = (String) context.get("ids");
	List<String> baseIdList = FastList.newInstance();
	if (UtilValidate.isNotEmpty(ids)) {
	    baseIdList = Arrays.asList(ids.split(";"));
	}
	Map<String, Object> result = ServiceUtil.returnSuccess();

	EntityCondition mainCond = null;
	List<EntityCondition> andExprs = FastList.newInstance();
	//andExprs.add(EntityCondition.makeCondition("parentProductId", EntityOperator.EQUALS, "_NA_"));
	if (UtilValidate.isNotEmpty(baseIdList))
	    andExprs.add(EntityCondition.makeCondition("id", EntityOperator.IN, baseIdList));
	if (andExprs.size() > 0)
	    mainCond = EntityCondition.makeCondition(andExprs, EntityOperator.AND);

	try {
	    List<GenericValue> entityList = delegator.findList("HrBecomeFullMember", mainCond, null, null, null, true);

	    List<Map<String, Object>> list = FastList.newInstance();
	    Map<String, Object> keyMap = FastMap.newInstance();
	    keyMap.put("0", "id");
	    keyMap.put("1", "partyId_dicText");
	    keyMap.put("2", "departmentId_dicText");
	    keyMap.put("3", "jobId");
	    keyMap.put("4", "becomeDate");
	    keyMap.put("5", "description");
	    keyMap.put("6", "statusId_dicText");
	    String headers = "编号,员工姓名,部门,职位,转正日期,转正原因,状态";
	    result.put("headers", headers);
	    
	    
	    

	    for (GenericValue entity : entityList) {
		Map<String, Object> map = FastMap.newInstance();
		String id = entity.getString("id");
		String partyId = entity.getString("partyId");
		String departmentId = entity.getString("departmentId");
		String jobId = entity.getString("jobId");
		Timestamp becomeDate = entity.getTimestamp("becomeDate");
		String description = entity.getString("description");
		String statusId = entity.getString("statusId");
		
		
		GenericValue person = delegator.findOne("Person", UtilMisc.toMap("partyId", partyId), false);
	    String partyId_dicText = person.getString("name");
		List<GenericValue> hrDepartmentInfo = delegator.findByAnd("HrDepartment", UtilMisc.toMap("departmentId", departmentId),null, false);
		GenericValue hrDepartment =EntityUtil.getFirst(hrDepartmentInfo);
		String departmentId_dicText = hrDepartment.getString("name");
		
	    String statusId_dicText = ExcelPoiUtils.getDataDictionaryDescription(delegator, "APPLY_STATUS",statusId);
		
	    map.put("id", id);
		map.put("partyId_dicText", partyId_dicText);
		map.put("departmentId_dicText", departmentId_dicText);
		map.put("jobId", jobId);
		map.put("becomeDate", becomeDate);
		map.put("description", description);
		map.put("statusId_dicText", statusId_dicText);

		list.add(map);
	    }
	    result.put("list", list);
	    result.put("keyMap", keyMap);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	return result;
    }

    
}
